package com.jinmei.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.jinmei.model.WorkSchedule;
import com.jinmei.web.CalendarTask;

public class WorkScheduleUtil {
	
	public static List<CalendarTask> convertCalendarTasks(List<WorkSchedule> workSchedules){
		
		Map<Date, Map<String, List<WorkSchedule>>> workScheduleMap = getWorkScheduleMap(workSchedules);
		
		List<CalendarTask> tasks = new ArrayList<CalendarTask>();
		
		//convert to tasks
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(Map.Entry<Date, Map<String, List<WorkSchedule>>> dayMap: workScheduleMap.entrySet()){
			
			Map<String, List<WorkSchedule>> classMap = dayMap.getValue();
			
			List<WorkSchedule> classAList = classMap.get("classA");
			if(classAList != null && !classAList.isEmpty()){
				CalendarTask task = new CalendarTask();
				int id = classAList.get(0).getEmployee().getfId(); //TODO: id need to be consider
				//TODO:string resource
				String title = "A班: " + classAList.get(0).getEmployee().getfName();
				
				if(classAList.size() > 1){
					title = title + "(" + classAList.size() +")";
				}
				
				String start = sdf.format(classAList.get(0).getWorkDate());
				task.setId(classAList.get(0).getWorkingType());
				task.setTitle(title);
				task.setStart(start);
				tasks.add(task);
			}
			
			List<WorkSchedule> classBList = classMap.get("classB");
			if(classBList != null && !classBList.isEmpty()){
				CalendarTask task = new CalendarTask();
				int id = classBList.get(0).getEmployee().getfId();
				String title = "B班: " + classBList.get(0).getEmployee().getfName();
				if(classBList.size() > 1){
					title = title + "(" + classBList.size() +")";
				}
				String start = sdf.format(classBList.get(0).getWorkDate());
				task.setId(classBList.get(0).getWorkingType());
				task.setTitle(title);
				task.setStart(start);
				tasks.add(task);
			}
			
			List<WorkSchedule> classCList = classMap.get("classC");
			if(classCList != null && !classCList.isEmpty()){
				CalendarTask task = new CalendarTask();
				int id = classCList.get(0).getEmployee().getfId();
				String title = "C班: " + classCList.get(0).getEmployee().getfName();
				if(classCList.size() > 1){
					title = title + "(" + classCList.size() +")";
				}
				String start = sdf.format(classCList.get(0).getWorkDate());
				task.setId(classCList.get(0).getWorkingType());
				task.setTitle(title);
				task.setStart(start);
				tasks.add(task);
			}
		}
		
		return tasks;
	}

	public static Map<Date, Map<String, List<WorkSchedule>>> getWorkScheduleMap(List<WorkSchedule> workSchedules) {
		
		List<CalendarTask> calendarTasks= new ArrayList<CalendarTask>();
		
		//key: workDate  value: have the same workDate collection
		Map<Date, List<WorkSchedule>> workScheduleDayMap = new HashMap<Date, List<WorkSchedule>>();
		
		for(WorkSchedule workSchedule : workSchedules){
			
			if(workScheduleDayMap.containsKey(workSchedule.getWorkDate())){
				List<WorkSchedule> workScheduleDayList = workScheduleDayMap.get(workSchedule.getWorkDate());
				workScheduleDayList.add(workSchedule);
			}else{
				List<WorkSchedule> workScheduleDayList = new ArrayList<WorkSchedule>();
				workScheduleDayList.add(workSchedule);
				workScheduleDayMap.put(workSchedule.getWorkDate(), workScheduleDayList);
			}
		}
		
		
		Map<Date, Map<String, List<WorkSchedule>>> mapLast = new HashMap<Date, Map<String, List<WorkSchedule>>>();
		
		for(Map.Entry<Date, List<WorkSchedule>> dayMap: workScheduleDayMap.entrySet()){
			
			//same day workSchedule
			List<WorkSchedule> workScheduleDay = dayMap.getValue();
			//class a workSchedule for one day
			List<WorkSchedule> classASchedule = new ArrayList<WorkSchedule>();
			List<WorkSchedule> classBSchedule = new ArrayList<WorkSchedule>();
			List<WorkSchedule> classCSchedule = new ArrayList<WorkSchedule>();
			
			Map<String, List<WorkSchedule>> classWorkMap = new HashMap<String, List<WorkSchedule>>(); 
			
			for(Iterator<WorkSchedule> it = workScheduleDay.iterator(); it.hasNext();){
				WorkSchedule workSchedule = it.next();
				switch(workSchedule.getWorkingType()){
					case 0:
						//class A
						classASchedule.add(workSchedule);
						break;
					case 1:
						//class B
						classBSchedule.add(workSchedule);
						break;
					case 2:
						//calss C
						classCSchedule.add(workSchedule);
						break;
				}
			}
			classWorkMap.put("classA", classASchedule);
			classWorkMap.put("classB", classBSchedule);
			classWorkMap.put("classC", classCSchedule);
			mapLast.put(dayMap.getKey(), classWorkMap);
		}
		
		return mapLast;
	}
	
	public static void main(String[] args){
		
		List<WorkSchedule> workSchedules = new ArrayList<WorkSchedule>();
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); 
		
		WorkSchedule ws1 = new WorkSchedule();
		ws1.setId(new Long(1));
		ws1.setWorkingType(0);
		try {
			ws1.setWorkDate(format1.parse("2013-10-18"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		WorkSchedule ws2 = new WorkSchedule();
		ws2.setId(new Long(2));
		ws2.setWorkingType(1);
		try {
			ws2.setWorkDate(format1.parse("2013-10-18"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		WorkSchedule ws3 = new WorkSchedule();
		ws3.setId(new Long(3));
		ws3.setWorkingType(2);
		try {
			ws3.setWorkDate(format1.parse("2013-10-18"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		WorkSchedule ws4 = new WorkSchedule();
		ws4.setId(new Long(4));
		ws4.setWorkingType(0);
		try {
			ws4.setWorkDate(format1.parse("2013-10-20"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		WorkSchedule ws5 = new WorkSchedule();
		ws5.setId(new Long(5));
		ws5.setWorkingType(1);
		try {
			ws5.setWorkDate(format1.parse("2013-10-20"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		WorkSchedule ws6 = new WorkSchedule();
		ws6.setId(new Long(6));
		ws6.setWorkingType(0);
		try {
			ws6.setWorkDate(format1.parse("2013-10-21"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		workSchedules.add(ws1);
		workSchedules.add(ws2);
		workSchedules.add(ws3);
		workSchedules.add(ws4);
		workSchedules.add(ws5);
		workSchedules.add(ws6);
		
		Map<Date, Map<String, List<WorkSchedule>>> maps = WorkScheduleUtil.getWorkScheduleMap(workSchedules);
		
		for(Map.Entry<Date, Map<String, List<WorkSchedule>>> dayMap: maps.entrySet()){
			System.out.println("Day " + dayMap.getKey().toString());
			
			Map<String, List<WorkSchedule>> classMap = dayMap.getValue();
			
			System.out.println("=======classA schedule=========");
			List<WorkSchedule> classAList = classMap.get("classA");
			for(WorkSchedule ws : classAList){
				System.out.println(ws.getId());
			}
			System.out.println("=======classB schedule=========");
			List<WorkSchedule> classBList = classMap.get("classB");
			for(WorkSchedule ws : classBList){
				System.out.println(ws.getId());
			}
			System.out.println("=======classC schedule=========");
			List<WorkSchedule> classCList = classMap.get("classC");
			for(WorkSchedule ws : classCList){
				System.out.println(ws.getId());
			}
		}
	}
}
